import json
import requests
from parse_url import parse_url
from pprint import pprint

url = "https://m.douban.com/rexxar/api/v2/subject_collection/movie_showing/items?start=0&count=18&loc_id=108288"
html_str = parse_url(url)

# json.loads把json字符串转化为python类型
ret1 = json.loads(html_str)
pprint(ret1) # 格式化输出（更美观）
print(type(ret1))

# json.dumps能够把python类型转化为json字符串
with open("douban.json","w",encoding="utf-8") as f:
    f.write(json.dumps(ret1,ensure_ascii=False,indent=4))
    # f.write(str(ret1))
"""
注意1：
    写入必须以字符串的形式：json.dumps(),
    ensure_ascii=False：中文部分正常显示
        encoding="utf-8"：写入内容是“utf-8”，必须以“utf-8”写入
    indent=4：格式化显示，缩进为4
注意2：
    为什么不使用eval，而使用json.dumps()？
        eval仅能处理字符串非常小，结构简单
"""

with open("douban.json","r",encoding="utf-8") as f:
    ret2 = f.read()
    ret3 = json.loads(ret2)
    print(ret3)
    print(type(ret3))
"""
注意3：写入的时候添加的参数，并不影响读取转换为python类型
注意4：json.dumps()和json.loads()互为逆操作
"""




# 使用json.load提取类文件对象中的数据
with open("douban.json","r",encoding="utf-8") as f:
    ret4 = json.load(f)
    print(ret4)
    print(type(ret4))

#json.dump能够把python类型放入类文件对象中
with open("douban1.json","w",encoding="utf-8") as f:
    json.dump(ret1,f,ensure_ascii=False,indent=2)